// 人类识别

const SecretId = "AKID97qLcfl0JL8fy3uWv62jGZeU5S5WalbC";
const SecretKey = "VCgv1sUs11mKdhX8VeicHitqZ5PVMzOY";
const tencentcloud = require("tencentcloud-sdk-nodejs-iai");

const IaiClient = tencentcloud.iai.v20180301.Client;
const clientConfig = {
  credential: {
    secretId: SecretId,
    secretKey: SecretKey,
  },
  region: "",
  profile: {
    httpProfile: {
      endpoint: "iai.tencentcloudapi.com",
    },
  },
};

const Base = require("../base.js");
var txyoss = require("./txyoss");

/*获取图片*/
module.exports = class extends Base {
  async __before() {
    this.client = new IaiClient(clientConfig);
  }
  //   验证人脸
  async indexAction() {
    const kxcer = this.post("imgurl");
    const jkcxcdf = await this.getdatse(kxcer);
    this.success(jkcxcdf);
  }
  //   对比人脸
  async duibirlAction() {
    const UrlA = this.post("UrlA");
    const UrlB = this.post("UrlB");
    const kmxcxr = await this.rldjsd(UrlA, UrlB);
    this.success(kmxcxr);
  }

  getdatse(ImageUrl) {
    return new Promise((resolve, reject) => {
      const params = {
        Url: ImageUrl,
      };
      this.client.DetectFace(params).then(
        (data) => {
          resolve({
            code: 0,
            ...data,
          });
        },
        (err) => {
          resolve({
            code: -1,
            msg: "图片中没有人脸",
            ...err,
          });
        }
      );
    });
  }

  rldjsd(UrlA, UrlB) {
    return new Promise((resolve, reject) => {
      const params = {
        UrlA,
        UrlB,
      };
      this.client.CompareFace(params).then(
        (data) => {
          let jnmx = 0;
          if (data.Score < 70) {
            jnmx = -1;
          }
          resolve({
            code: jnmx,
            ...data,
          });
        },
        (err) => {
          resolve({
            code: -1,
            msg: "图片中没有人脸",
            ...err,
          });
        }
      );
    });
  }
};
